﻿//***** Lấy dữ liệu từ *****
// Catalogue_Department
// Catalogue_SchoolYear
// Catalogue_Term
// Catalogue_Level
// Catalogue_Major
// Catalogue_TrainingType
// Training_Student_Class
// Training_Student
// Training_Student_Subject
// Training_Subject
// Training_Subject_Open
// Training_TrainingProgram
// Training_TrainingProgram_Detail

// ***** Lưu dữ liệu vào *****
// Training_Student_Subject

using System;
using System.Windows.Forms;
using UniMana.Utils.Data;
using UniMana.Data;
using System.Data;
using UniMana.Utils.Configs;
using DevExpress.XtraEditors;
using System.Collections.Generic;
using DevExpress.XtraGrid.Views.Grid;
using System.Drawing;

namespace UniMana.Win.Training
{
    public partial class FrmMultiRegist : DevExpress.XtraEditors.XtraForm
    {
        private DBMain m_data = new DBMain();
        private DataTable m_tblTable;
        private DataTable m_tblStudent;
        private DataTable m_tblStudentSubject;
        private int m_iTraining_SubjectKey = 0;
        private int m_iTrainingProgramKey = 0;
        private string m_strIDSubject = "";
        private string m_strNameSubject = "";
        
        private string m_strTraining_StudentKey;
        private int m_iTraining_ClassKey;

        List<string> listBold;

        private Training_Student_ClassInfo classinfo;

        public DevExpress.Utils.WaitDialogForm dlg;

        public FrmMultiRegist()
        {
            if (!SysUtils.CheckAccessByRule(RuleConfig.DKM))
                return;
            InitializeComponent();

            LoadData();
            rluCatalogue_Department.GetData(m_data.m_DCReadOnly.Catalogue_Departments, "Catalogue_DepartmentKey", "Name");
            rluCatalogue_Major1.GetData(m_data.m_DCReadOnly.Catalogue_Majors, "Catalogue_MajorKey", "Name");
            rluCatalogue_Major2.GetData(m_data.m_DCReadOnly.Catalogue_Majors, "Catalogue_MajorKey", "Name");
            rluTraining_TrainingProgram.GetData(m_data.m_DCReadOnly.Training_TrainingPrograms, "Training_TrainingProgramKey", "Name");

            rluTraining_Class1.GetData(m_data.m_DCReadOnly.Training_Classes, "Training_ClassKey", "Name");
            rluTraining_Class2.GetData(m_data.m_DCReadOnly.Training_Classes, "Training_ClassKey", "Name");
            rluCatalogue_Status.GetData(m_data.m_DCReadOnly.Catalogue_Status, "Catalogue_StatusKey", "Name");
        }

        /// <summary>
        /// Form load
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void FrmMultiRegist_Load(object sender, EventArgs e)
        {
            btnExportStudent.Text = "Xuất Excel DSSV" + "\n " + "chưa ĐK môn";
            btnExportStudentRegist.Text = "Xuất Excel DSSV" + "\n " + "đã đăng ký môn";
            listBold = new List<string>();
        }

        /// <summary>
        /// Hiển thị dữ liệu
        /// </summary>
        
        private void LoadData()
        {
            LoadSubject("");
        }

        /// <summary>
        /// Hàm trả về các môn đủ điều kiện
        /// </summary>

        private void LoadSubject(string m_strSubjectID)
        {
            m_tblTable = new DataTable();
            m_tblTable = (this.m_data.m_DCReadOnly.Training_Subject_Opens.GetSubjectOpen(m_data.m_DCReadOnly.Training_Subjects,
                m_data.m_DCReadOnly.Training_TrainingProgram_Details,
                m_strSubjectID)).ToDataTable();
            if (m_tblTable.Rows.Count > 0)
            {
                try
                {
                    DataView dview = new DataView(m_tblTable);
                    dview.Table.SortDataTable("Catalogue_DepartmentKey", true);

                    m_tblTable = new DataTable();
                    m_tblTable.Clear();

                    m_tblTable = dview.Table;
                    gctSubject.DataSource = m_tblTable;
                    grvSubject.ExpandAllGroups();
                }
                catch
                {
                    gctSubject.DataSource = m_tblTable;
                    grvSubject.ExpandAllGroups();
                }
            }
            else
            {
                XtraMessageBox.Show("Không có môn mở");
                m_iTraining_SubjectKey = 0;
                gctSubject.DataSource = null;
                gctStudent.DataSource = null;
                gctStudentSubject.DataSource = null;
            }
            grbSubjectList.Text = "Danh sách môn học có " + m_tblTable.Rows.Count.ToString() + " môn";
        }

        /// <summary>
        /// Khởi tạo table
        /// </summary>
        /// <param name="m_tbl"></param>

        private void InitalTable(ref DataTable m_tbl)
        {
            m_tbl.Rows.Clear();
            m_tbl.Columns.Clear();
            m_tbl.Columns.Add("Catalogue_MajorKey");
            m_tbl.Columns.Add("Training_ClassKey");
            m_tbl.Columns.Add("Training_StudentKey");
            m_tbl.Columns.Add("ID");
            m_tbl.Columns.Add("LastName");
            m_tbl.Columns.Add("SureName");
            m_tbl.Columns.Add("Birthday");
            m_tbl.Columns.Add("Gender");
            m_tbl.Columns.Add("Catalogue_StatusKey");
        }

        /// <summary>
        /// Hàm lấy các sinh viên thỏa điều kiện đã đăng ký môn học
        /// </summary>

        private void LoadStudentSubject()
        {
            m_tblStudentSubject = new DataTable();
            InitalTable(ref m_tblStudentSubject);
            if (txtID.Text == "")
            
            m_tblStudentSubject = (this.m_data.m_DCReadOnly.Training_Students.GetStudentListRegistSubject(m_data.m_DCReadOnly.Training_Subject_Opens,
                m_data.m_DCReadOnly.Training_Student_Classes,
                m_data.m_DCReadOnly.Training_Student_Subjects,
                m_iTraining_SubjectKey,
                txtID.Text,
                m_iTrainingProgramKey,
                m_data.m_DCReadOnly.Training_Subject_Opens.GetSubjectOpenKey(m_iTraining_SubjectKey))).ToDataTable();
           
            if (m_tblStudentSubject.Rows.Count != 0)
            {
                gctStudentSubject.DataSource = m_tblStudentSubject;
                grvStudentSubject.ExpandAllGroups();
                grbStudentSubjectList.Text = "Danh sách sinh viên đăng ký môn " + m_strNameSubject + " có " + m_tblStudentSubject.Rows.Count.ToString() + " sinh viên";
            }
            else
            {
                InitalTable(ref m_tblStudentSubject);
                gctStudentSubject.DataSource = null;
                grbStudentSubjectList.Text = "Danh sách sinh viên đăng ký môn " + m_strNameSubject + " có 0 sinh viên";
            }
        }

        /// <summary>
        /// Hàm lấy các sinh viên thỏa điều kiện chưa đăng ký môn học
        /// </summary>

        private void LoadStudent()
        {
            m_tblStudent = new DataTable();
            InitalTable(ref m_tblStudent);

            int adsfsd = int.Parse(m_data.m_DCReadOnly.Training_Subject_Opens.GetSubjectOpenKey(m_iTraining_SubjectKey).ToString());

            m_tblStudent = (this.m_data.m_DCReadOnly.Training_Students.GetStudentListNotRegistSubject(m_data.m_DCReadOnly.Training_TrainingProgram_Details,
                m_data.m_DCReadOnly.Training_Student_Classes,
                m_data.m_DCReadOnly.Training_Student_Subjects,
                m_data.m_DCReadOnly.Training_Student_Subject_Classes,
                m_data.m_DCReadOnly.Training_Subject_Classes,
                m_iTraining_SubjectKey,
                m_data.m_DCReadOnly.Training_Subject_Opens.GetSubjectOpenKey(m_iTraining_SubjectKey),
                txtID.Text,
                m_iTrainingProgramKey)).ToDataTable();

            if (m_tblStudent.Rows.Count != 0)
            {
                gctStudent.DataSource = m_tblStudent;
                grvStudent.ExpandAllGroups();
                grbStudentList.Text = "Danh sách sinh viên có " + m_tblStudent.Rows.Count.ToString() + " sinh viên";
            }
            else
            {
                InitalTable(ref m_tblStudent);
                gctStudent.DataSource = null;
                grbStudentList.Text = "Danh sách sinh viên có 0 sinh viên";
            }
        }

        /// <summary>
        /// Chọn môn học cho sinh viên đăng ký
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        
        private void gctSubject_Click(object sender, EventArgs e)
        {
            try
            {
                dlg = new DevExpress.Utils.WaitDialogForm(MsgConfig.MSG_WAIT, MsgConfig.MSG_LOADING);
                m_iTraining_SubjectKey = int.Parse(grvSubject.GetFocusedRowCellValue("Training_SubjectKey").ToString());
                m_iTrainingProgramKey = int.Parse(grvSubject.GetFocusedRowCellValue("Training_TrainingProgramKey").ToString());
                m_strIDSubject = grvSubject.GetFocusedRowCellValue("ID").ToString();
                m_strNameSubject = grvSubject.GetFocusedRowCellValue("Name").ToString();
                grbStudentSubjectList.Text = "Danh sách sinh viên đăng ký môn : " + m_strNameSubject;
                
                gctStudent.DataSource = null;
                gctStudentSubject.DataSource = null;

                LoadStudentSubject();
                LoadStudent();

                dlg.Close();
            }
            catch { dlg.Close(); }
        }

        /// <summary>
        /// Hàm kiểm tra insert dữ liệu vào cơ sở dữ liệu với Key sinh viên, Key môn học, học kỳ, năm học
        /// </summary>
        /// <returns></returns>

        private bool CheckInsert(string m_strTraining_StudentKey, int m_iTraining_SubjectKey, int m_iCatalogue_TermKey, int m_iCatalogue_SchoolYearKey)
        {
            m_tblTable = new DataTable();
            //m_tblTable = (m_data.m_DCReadOnly.Training_Student_Subjects.GetByStudentSubject(m_data.m_DCReadOnly.Training_Subject_Classes,
            //    m_strTraining_StudentKey,
            //    m_iTraining_SubjectKey,
            //    m_iCatalogue_TermKey,
            //    m_iCatalogue_SchoolYearKey)).ToDataTable();
            m_tblTable = (m_data.m_DCReadOnly.Training_Student_Subjects.GetByStudentSubject(m_data.m_DCReadOnly.Training_Subject_Classes,
                m_strTraining_StudentKey,
                m_iTraining_SubjectKey,
                m_iCatalogue_TermKey,
                m_iCatalogue_SchoolYearKey)).ToDataTable();
            if (m_tblTable.Rows.Count != 0)
                return false;
            else
                return true;
        }

        /// <summary>
        /// Kiểm tra môn học đang mở trước khi lưu
        /// </summary>
        /// <param name="m_iTraining_SubjectKey"></param>
        /// <returns></returns>

        private bool ChecKSubjectOpen(int m_iTraining_SubjectKey)
        {
            try
            {
                int check = m_data.m_DCReadOnly.Training_Subject_Opens.CheckSubjectOpen(m_iTraining_SubjectKey);
                if (check == 1)
                    return true;
                return false;
            }
            catch
            { return false; }
        }

        /// <summary>
        /// Chọn sinh viên đăng ký vào môn học
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                if (ckeStudent.Checked)
                    listBold = new List<string>();
                dlg = new DevExpress.Utils.WaitDialogForm(MsgConfig.MSG_WAIT, MsgConfig.MSG_LOADING);
                if ((gctSubject.DataSource != null) && (grvSubject.SelectedRowsCount > 0))
                {
                    if (ChecKSubjectOpen(m_iTraining_SubjectKey))
                    {
                        if (grvStudent.SelectedRowsCount > 0)
                        {
                            int[] index = grvStudent.GetSelectedRows();
                            int _d = 0;
                            for (int i = 0; i < index.Length; i++)
                            {
                                if (index[i] >= 0)
                                {
                                    index[i] = index[i] - _d;
                                    m_strTraining_StudentKey = grvStudent.GetRowCellValue(index[i], "Training_StudentKey").ToString();
                                    m_iTraining_ClassKey = int.Parse(grvStudent.GetRowCellValue(index[i], "Training_ClassKey").ToString());
                                    classinfo = new Training_Student_ClassInfo(m_strTraining_StudentKey,
                                        m_iTraining_ClassKey,
                                        "nguyen.tuanan");
                                    //insert vào bảng Training_Student_Subject
                                    Training_Student_SubjectInfo sub = new Training_Student_SubjectInfo(m_strTraining_StudentKey,
                                        m_iTraining_SubjectKey,
                                        0,
                                        m_iTrainingProgramKey,
                                        m_data.m_DCReadOnly.Training_Subject_Opens.GetSubjectOpenKey(m_iTraining_SubjectKey));
                                    try
                                    {
                                        if (sub.Properties.Training_SubjectKey > 0)
                                        {
                                            gctStudent.DataSource = null;
                                            gctStudentSubject.DataSource = null;
                                            LoadStudentSubject();
                                            LoadStudent();
                                        }
                                        else
                                        {
                                            sub.Properties.Training_StudentKey = Guid.Parse(m_strTraining_StudentKey);
                                            sub.Properties.Training_SubjectKey = m_iTraining_SubjectKey;
                                            sub.Properties.RegistTime = SysDateTime.Now;
                                            sub.Properties.TotalCredits = m_data.m_DCReadOnly.Training_Subjects.GetTotalCreditsOfSubject(m_iTraining_SubjectKey);
                                            sub.Properties.Training_Student_Subject_Regist_TypeKey = DefaultConfig.Score_FirstCore; // đăng ký lần đầu
                                            sub.Properties.Training_SubjectOpenKey = m_data.m_DCReadOnly.Training_Subject_Opens.GetSubjectOpenKey(m_iTraining_SubjectKey);
                                            sub.Properties.Training_TrainingProgramKey = m_iTrainingProgramKey;
                                            sub.Save();
                                            LeftToRight(m_strTraining_StudentKey, m_iTraining_ClassKey);
                                            _d++;
                                            listBold.Add(m_strTraining_StudentKey);
                                        }
                                    }
                                    catch
                                    {
                                        sub.Properties.Training_StudentKey = Guid.Parse(m_strTraining_StudentKey);
                                        sub.Properties.Training_SubjectKey = m_iTraining_SubjectKey;
                                        sub.Properties.RegistTime = SysDateTime.Now;
                                        sub.Properties.TotalCredits = this.m_data.m_DCReadOnly.Training_Subjects.GetTotalCreditsOfSubject(m_iTraining_SubjectKey);
                                        sub.Properties.Training_Student_Subject_Regist_TypeKey = DefaultConfig.Score_FirstCore; // đăng ký lần đầu
                                        sub.Properties.Training_SubjectOpenKey = m_data.m_DCReadOnly.Training_Subject_Opens.GetSubjectOpenKey(m_iTraining_SubjectKey);
                                        sub.Properties.Training_TrainingProgramKey = m_iTrainingProgramKey;
                                        sub.Save();
                                        LeftToRight(m_strTraining_StudentKey, m_iTraining_ClassKey);
                                        _d++;
                                    }
                                }   
                            }
                            gctStudent.DataSource = m_tblStudent;
                            gctStudentSubject.DataSource = m_tblStudentSubject;
                            grvStudentSubject.ExpandAllGroups();
                        }
                        else
                            XtraMessageBox.Show("Chưa chọn sinh viên", "Thông báo");
                    }
                    else
                    {
                        dlg.Close();
                        DialogResult result = XtraMessageBox.Show("Môn học đã được đóng lại. Không thể đăng ký" + "\n" + "Bạn có muốn làm mới lại dữ liệu", "Thông báo", MessageBoxButtons.YesNo);
                        if (result == DialogResult.Yes)
                        {
                            LoadSubject(txtIDSubject.Text);
                            gctStudent.DataSource = null;
                            gctStudentSubject.DataSource = null;
                            goto cccc;
                        }
                    }
                }
                else
                {
                    XtraMessageBox.Show("Chưa có môn học để sinh viên đăng ký");
                }
                dlg.Close();
            cccc: string asd = "";
            }
            catch { }
        }

        /// <summary>
        /// Xóa sinh viên đã đăng ký môn học
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void btnRemove_Click(object sender, EventArgs e)
        {
            if (grvStudentSubject.SelectedRowsCount > 0)
            {
                dlg = new DevExpress.Utils.WaitDialogForm(MsgConfig.MSG_WAIT, MsgConfig.MSG_LOADING);
                int[] index = grvStudentSubject.GetSelectedRows();
                int _d = 0;
                for (int i = 0; i < index.Length; i++)
                {
                    if (index[i] >= 0)
                    {
                        index[i] = index[i] - _d;
                        m_strTraining_StudentKey = grvStudentSubject.GetRowCellValue(index[i], "Training_StudentKey").ToString();
                        m_iTraining_ClassKey = int.Parse(grvStudentSubject.GetRowCellValue(index[i], "Training_ClassKey").ToString());

                        if (ChecKSubjectOpen(m_iTraining_SubjectKey))
                        {
                            // Xóa record sinh viên đăng ký môn học trong bang Training_Student_Subject
                            Training_Student_SubjectInfo studentsubject = new Training_Student_SubjectInfo(m_strTraining_StudentKey,
                                m_iTraining_SubjectKey,
                                0,
                                m_iTrainingProgramKey,
                                m_data.m_DCReadOnly.Training_Subject_Opens.GetSubjectOpenKey(m_iTraining_SubjectKey));
                            if (studentsubject.Properties.Training_Student_SubjectKey > 0)
                            {
                                studentsubject.Delete();
                                RightToLeft(m_strTraining_StudentKey, m_iTraining_ClassKey);
                                _d++;
                            }
                            else
                                XtraMessageBox.Show("Không thể hủy đăng ký cho sinh viên " + grvStudentSubject.GetRowCellValue(index[i], "LastName").ToString() + " " + grvStudentSubject.GetRowCellValue(index[i], "SureName").ToString());
                        }
                        else
                        {
                            dlg.Close();
                            DialogResult result = XtraMessageBox.Show("Môn học đã được đóng lại. Không thể hủy đăng ký" + "\n" + "Bạn có muốn làm mới lại dữ liệu", "Thông báo", MessageBoxButtons.YesNo);
                            if (result == DialogResult.Yes)
                            {
                                LoadSubject(txtIDSubject.Text);
                                gctStudent.DataSource = null;
                                gctStudentSubject.DataSource = null;
                                goto cccc;
                            }
                        }
                    }
                }
                gctStudent.DataSource = m_tblStudent;
                gctStudentSubject.DataSource = m_tblStudentSubject;
                grvStudent.ExpandAllGroups();
                dlg.Close();
            }
            else
                XtraMessageBox.Show("Chưa chọn sinh viên", "Thông báo");
        cccc: string asd = "";
        }

        /// <summary>
        /// Di chuyển mẫu tin từ danh sách sinh viên sang danh sách sinh viên đăng ký môn học
        /// </summary>
        /// <param name="m_strTraining_StudentKey"></param>
        /// <param name="m_iTraining_ClassKey"></param>

        private void LeftToRight(string m_strTraining_StudentKey, int m_iTraining_ClassKey)
        {
            if (m_tblStudent.Rows.Count != 0)
            {
                for (int i = 0; i < m_tblStudent.Rows.Count; i++)
                {
                    if ((m_tblStudent.Rows[i]["Training_StudentKey"].ToString() == m_strTraining_StudentKey) && (m_tblStudent.Rows[i]["Training_ClassKey"].ToString() == m_iTraining_ClassKey.ToString()))
                    {
                        m_tblStudentSubject.Rows.Add(m_tblStudent.Rows[i]["Catalogue_MajorKey"], m_tblStudent.Rows[i]["Training_ClassKey"], m_tblStudent.Rows[i]["Training_StudentKey"], m_tblStudent.Rows[i]["ID"], m_tblStudent.Rows[i]["LastName"], m_tblStudent.Rows[i]["SureName"], m_tblStudent.Rows[i]["Birthday"], m_tblStudent.Rows[i]["Gender"], m_tblStudent.Rows[i]["Catalogue_StatusKey"]);
                        m_tblStudent.Rows.RemoveAt(i);
                        return;
                    }
                }
            }
        }

        /// <summary>
        /// Di chuyển mẫu tin từ danh sách sinh viên đăng ký môn học sang danh sách sinh viên
        /// </summary>
        /// <param name="m_strTraining_StudentKey"></param>
        /// <param name="m_iTraining_ClassKey"></param>

        private void RightToLeft(string m_strTraining_StudentKey, int m_iTraining_ClassKey)
        {
            if (m_tblStudentSubject.Rows.Count != 0)
            {
                for (int i = 0; i < m_tblStudentSubject.Rows.Count; i++)
                {
                    if ((m_tblStudentSubject.Rows[i]["Training_StudentKey"].ToString() == m_strTraining_StudentKey) && (m_tblStudentSubject.Rows[i]["Training_ClassKey"].ToString() == m_iTraining_ClassKey.ToString()))
                    {
                        m_tblStudent.Rows.Add(m_tblStudentSubject.Rows[i]["Catalogue_MajorKey"], m_tblStudentSubject.Rows[i]["Training_ClassKey"], m_tblStudentSubject.Rows[i]["Training_StudentKey"], m_tblStudentSubject.Rows[i]["ID"], m_tblStudentSubject.Rows[i]["LastName"], m_tblStudentSubject.Rows[i]["SureName"], m_tblStudentSubject.Rows[i]["Birthday"], m_tblStudentSubject.Rows[i]["Gender"], m_tblStudentSubject.Rows[i]["Catalogue_StatusKey"]);
                        m_tblStudentSubject.Rows.RemoveAt(i);
                        return;
                    }
                }
            }
        }

        /// <summary>
        /// Tìm 1 sinh viên
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void btnSearch_Click(object sender, EventArgs e)
        {
            try
            {
                if (m_iTraining_SubjectKey != 0)
                {
                    LoadStudent();
                    LoadStudentSubject();
                    if ((gctStudent.DataSource == null) && (gctStudentSubject.DataSource == null))
                        XtraMessageBox.Show("Không tìm thấy mã sinh viên");
                }
                else
                    XtraMessageBox.Show("Chưa chọn môn đăng ký");
            }
            catch { }
        }

        /// <summary>
        /// Sự kiện enter tìm sinh viên
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void txtID_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.KeyCode == Keys.Enter)
                {
                    if (m_iTraining_SubjectKey != 0)
                    {
                        LoadStudent();
                        LoadStudentSubject();
                        if ((gctStudent.DataSource == null) && (gctStudentSubject.DataSource == null))
                            XtraMessageBox.Show("Không tìm thấy mã sinh viên");
                    }
                    else
                        XtraMessageBox.Show("Chưa chọn môn đăng ký");
                }
            }
            catch { }
        }

        /// <summary>
        /// Xóa mã môn học
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void lbcClear_Click(object sender, EventArgs e)
        {
            txtID.Text = "";
        }

        /// <summary>
        /// Xem thông tin sinh viên
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void grvStudent_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                if ((!grvStudent.IsFilterRow(grvStudent.FocusedRowHandle)) && (!grvStudent.IsGroupRow(grvStudent.FocusedRowHandle)))
                {
                    DevExpress.Utils.WaitDialogForm dlg = new DevExpress.Utils.WaitDialogForm(MsgConfig.MSG_WAIT, MsgConfig.MSG_LOADING);
                    string currentKey = grvStudent.GetFocusedRowCellValue("Training_StudentKey").ToString();
                    int currentMajor = int.Parse(grvStudent.GetFocusedRowCellValue("Catalogue_MajorKey").ToString());
                    FrmStudent_SubInfo frmInfo = new FrmStudent_SubInfo();
                    Training_StudentInfo studentInfo = new Training_StudentInfo(currentKey);
                    Training_Student_CheckListInfo studentCheckListInfo = new Training_Student_CheckListInfo(currentKey);
                    Training_Student_ClassInfo studentClass = new Training_Student_ClassInfo("nguyen.tuanan", currentKey, currentMajor, true);

                    frmInfo.m_StudentInfo = studentInfo;
                    frmInfo.m_CheckListInfo = studentCheckListInfo;
                    frmInfo.m_StudentClass = studentClass;

                    frmInfo.m_Flag = false;
                    dlg.Close();

                    frmInfo.ShowDialog();
                }
            }
            catch (Exception ex) { XtraMessageBox.Show(ex.Message); }
        }

        /// <summary>
        /// Xem thông tin sinh viên
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void grvStudentSubject_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                if ((!grvStudentSubject.IsFilterRow(grvStudentSubject.FocusedRowHandle)) && (!grvStudentSubject.IsGroupRow(grvStudentSubject.FocusedRowHandle)))
                {
                    DevExpress.Utils.WaitDialogForm dlg = new DevExpress.Utils.WaitDialogForm(MsgConfig.MSG_WAIT, MsgConfig.MSG_LOADING);
                    string currentKey = grvStudentSubject.GetFocusedRowCellValue("Training_StudentKey").ToString();
                    int currentMajor = int.Parse(grvStudentSubject.GetFocusedRowCellValue("Catalogue_MajorKey").ToString());
                    FrmStudent_SubInfo frmInfo = new FrmStudent_SubInfo();
                    Training_StudentInfo studentInfo = new Training_StudentInfo(currentKey);
                    Training_Student_CheckListInfo studentCheckListInfo = new Training_Student_CheckListInfo(currentKey);
                    Training_Student_ClassInfo studentClass = new Training_Student_ClassInfo("nguyen.tuanan", currentKey, currentMajor, true);

                    frmInfo.m_StudentInfo = studentInfo;
                    frmInfo.m_CheckListInfo = studentCheckListInfo;
                    frmInfo.m_StudentClass = studentClass;

                    frmInfo.m_Flag = false;
                    dlg.Close();

                    frmInfo.ShowDialog();
                }
            }
            catch (Exception ex) { XtraMessageBox.Show(ex.Message); }
        }

        /// <summary>
        /// Tìm môn học
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void btnSearchSubject_Click(object sender, EventArgs e)
        {
            try
            {
                LoadSubject(txtIDSubject.Text);
            }
            catch { }
        }

        /// <summary>
        /// Phím tắt tìm môn học
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void txtIDSubject_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.KeyCode == Keys.Enter)
                    LoadSubject(txtIDSubject.Text);
            }
            catch { }
        }

        /// <summary>
        /// Xóa mã môn
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void lbcClearSubject_Click(object sender, EventArgs e)
        {
            txtIDSubject.Text = "";
        }

        /// <summary>
        /// Xem danh sách sinh viên nợ môn hoặc cải thiện điểm
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void BtnViewStudentListRegistSubjectAgain_Click(object sender, EventArgs e)
        {
            if (m_iTraining_SubjectKey != 0)
            {
                FrmMultiRegistAgain mula = new FrmMultiRegistAgain(m_iTraining_SubjectKey, m_iTrainingProgramKey);
                mula.ShowDialog();
            }
            else
            {
                FrmMultiRegistAgain mula = new FrmMultiRegistAgain(0, 0);
                mula.ShowDialog();
            }
        }

        /// <summary>
        /// Xuất sinh viên chưa đăng ký môn
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void btnExportStudent_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFile = new SaveFileDialog();
            saveFile.Filter = "*.xlsx | *.xls";
            saveFile.Title = "Lưu tập tin excel 2007";
            if (saveFile.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                grvStudent.ExportToXlsx(saveFile.FileName);
            }
        }

        /// <summary>
        /// Xuất sinh viên đã đăng ký môn
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void btnExportStudentRegist_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFile = new SaveFileDialog();
            saveFile.Filter = "*.xlsx | *.xls";
            saveFile.Title = "Lưu tập tin excel 2007";
            if (saveFile.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                grvStudentSubject.ExportToXlsx(saveFile.FileName);
            }
        }

        /// <summary>
        /// Tô màu các sinh viên mới đăng ký
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void grvStudentSubject_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e)
        {
            if (listBold.Count > 0)
            {
                GridView View = sender as GridView;
                if (!View.IsFilterRow(View.FocusedRowHandle))
                {
                    string studentkey = Convert.ToString(View.GetRowCellValue(e.RowHandle, View.Columns["Training_StudentKey"]));
                    for (int i = 0; i < listBold.Count; i++)
                        if (studentkey == listBold[i].ToString())
                            e.Appearance.BackColor = Color.LightBlue;
                }
            }
        }
    }
}